<template>
  <view class="cart-container" v-if="cart.length">
    <!-- 选择收获地址 -->
    <my-address></my-address>
    <!-- 1. 购物车标题 -->
    <view class="cart-title">
      <uni-icons type="shop" size="18"></uni-icons>
      <text class="cart-text">购物车</text>
    </view>
    <!-- 2.购物车列表 -->
    <uni-swipe-action>
      <block v-for="(item,i) in cart" :key="i">
        <uni-swipe-action-item :right-options="options" @click="bindClick(item)">
          <my-goods :showNum="true" :show-radio="true" :goodinfo="item" @radio-change="radioChange" @num-change="numChange"></my-goods>
        </uni-swipe-action-item>
      </block>
    </uni-swipe-action>
  
  <!-- 3.结算 -->
  <my-settle></my-settle>
  </view>
  <view class="empty-cart" v-else>
      <image src="/static/cart_empty@2x.png" class="pic"></image>
      <view class="txt-tip">
        空空如也~
      </view>
  </view>
</template>

<script>
  import mixBadge from '@/mixins/tabbar-badge.js'
  import {
    mapState,
    mapMutations
  } from 'vuex';
  export default {
    mixins: [mixBadge],
    data() {
      return {
        options: [{
          text: '删除',
          style: {
            backgroundColor: '#c00000'
          }
        }]
      };
    },
    computed: {
      ...mapState('m_cart', ['cart'])
    },
    methods: {
      ...mapMutations('m_cart', ['updateGoodState', 'updateGoodCount','removeGoodById']),
      // 更改选中状态
      radioChange(goods) {
        this.updateGoodState(goods.goods_id);
      },
      // 更改数量
      numChange(goods) {
        // console.log(goods); 
        this.updateGoodCount(goods)
      },
      // 删除商品
      bindClick(goods) {
       // console.log(goods);  
       this.removeGoodById(goods);
      }
    }
  }
</script>

<style lang="scss">
  page {
    background-color: #fff;
    .cart-container {
      padding-bottom: 50px;
      .cart-title {
        height: 40px;
        line-height: 40px;
        padding-left: 5px;
      
        uni-icons {
          vertical-align: middle;
        }
      
        border-bottom: 1px solid #efefef;
      
        .cart-text {
          margin-left: 5px;
        }
      }
    }
    .empty-cart {
       display: flex;
       flex-direction: column;
       align-items: center;
       padding-top: 300rpx;
       .pic {
         width: 160rpx;
         height: 160rpx;
       }
       .txt-tip {
         font-size: 12px;
         color: gray;
         margin-top: 10px;
       }
    }
  }
</style>
